package com.xms.ribbonbutton.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.ribbonbutton.core.RibbonButtonArea;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.ribbonbutton.service.IRibbonButtonFinderService;
import com.xms.ribbonbutton.service.IRibbonButtonUpdaterService;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.security.core.PrivilegeResourceItem;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.UpdateAuthorizationStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * RibbonButtonAuthorizationController
 * </p>
 *
 * @author migo
 * @since 2021/10/19
 */
@RestController
@RequestMapping("{org}/api/ribbonbutton")
public class RibbonButtonAuthorizationController extends ApiBaseController {
    private IEntityFinderService _entityFinderService;
    private IRibbonButtonFinderService _ribbonButtonFinderService;
    private IRibbonButtonUpdaterService _ribbonButtonUpdaterService;
    @Autowired
    public void RibbonButtonAuthorizationController(IEntityFinderService entityFinderService,
                                                    IRibbonButtonFinderService ribbonButtonFinderService,
                                                    IRibbonButtonUpdaterService ribbonButtonUpdaterService) {
        _entityFinderService = entityFinderService;
        _ribbonButtonFinderService = ribbonButtonFinderService;
        _ribbonButtonUpdaterService = ribbonButtonUpdaterService;
    }

    @ApiOperation("查询按钮权限资源")
    @RequestMapping(value = "privilegeresource", method = RequestMethod.GET)
    public JsonResultObject privilegeResource(@RequestParam(value = "authorizationenabled", required = false) Boolean authorizationEnabled)
    {
        LambdaQueryWrapper<RibbonButton> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RibbonButton::getStatecode, true);
        if (authorizationEnabled != null)
        {
            queryWrapper.eq(RibbonButton::getAuthorizationenabled, authorizationEnabled);
        }
        queryWrapper.select(RibbonButton::getRibbonbuttonid, RibbonButton::getLabel, RibbonButton::getShowarea, RibbonButton::getEntityid, RibbonButton::getAuthorizationenabled);
        List<RibbonButton> data = _ribbonButtonFinderService.list(queryWrapper);
        if (CollectionUtil.notEmpty(data))
        {
            List<PrivilegeResourceItem> result = new ArrayList<>();
            List<Entity> entities = _entityFinderService.all();
            for (Entity item : entities)
            {
                List<RibbonButton> entityButtons = data.stream().filter(x -> x.getEntityid().equals(item.getEntityid())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(entityButtons))
                {
                    continue;
                }
                List<PrivilegeResourceItem> childrens = new ArrayList<>();
                PrivilegeResourceItem group1 = new PrivilegeResourceItem();
                group1.groupid = item.getEntityid();
                group1.label = item.getLocalizedname();
                List<RibbonButton> formButtons = entityButtons.stream().filter(x -> x.getShowarea() == RibbonButtonArea.Form).collect(Collectors.toList());
                if (CollectionUtil.notEmpty(formButtons))
                {
                    List<PrivilegeResourceItem> formButtonsPrv = formButtons.stream().map(x -> new PrivilegeResourceItem(){{
                        id = x.getRibbonbuttonid();
                        label = x.getLabel();
                        authorizationenabled = x.getAuthorizationenabled();
                        groupid = x.getEntityid();
                    }}).collect(Collectors.toList());
                    PrivilegeResourceItem groupForm = new PrivilegeResourceItem()
                    {{
                        label = "表单";
                        children = formButtonsPrv;
                    }};
                    childrens.add(groupForm);
                }
                List<RibbonButton> listHeaderButtons = entityButtons.stream().filter(x -> x.getShowarea() == RibbonButtonArea.ListHead).collect(Collectors.toList());
                if (CollectionUtil.notEmpty(listHeaderButtons))
                {
                    List<PrivilegeResourceItem> listHeaderButtonsPrv = listHeaderButtons.stream().map(x -> new PrivilegeResourceItem(){{
                        id = x.getRibbonbuttonid();
                        label = x.getLabel();
                        authorizationenabled = x.getAuthorizationenabled();
                        groupid = x.getEntityid();
                    }}).collect(Collectors.toList());
                    PrivilegeResourceItem groupListHeader = new PrivilegeResourceItem()
                    {{
                        label = "列表头部";
                        children = listHeaderButtonsPrv;
                    }};
                    childrens.add(groupListHeader);
                }
                List<RibbonButton> listRowButtons = entityButtons.stream().filter(x -> x.getShowarea() == RibbonButtonArea.ListRow).collect(Collectors.toList());
                if (CollectionUtil.notEmpty(listRowButtons))
                {
                    List<PrivilegeResourceItem> listRowButtonsPrv = listRowButtons.stream().map(x -> new PrivilegeResourceItem(){{
                        id = x.getRibbonbuttonid();
                        label = x.getLabel();
                        authorizationenabled = x.getAuthorizationenabled();
                        groupid = x.getEntityid();
                    }}).collect(Collectors.toList());
                    PrivilegeResourceItem groupListRow = new PrivilegeResourceItem()
                    {{
                        label = "列表行内";
                        children = listRowButtonsPrv;
                    }};
                    childrens.add(groupListRow);
                }
                List<RibbonButton> subGridButtons = entityButtons.stream().filter(x -> x.getShowarea() == RibbonButtonArea.SubGrid).collect(Collectors.toList());
                if (CollectionUtil.notEmpty(subGridButtons))
                {
                    List<PrivilegeResourceItem> subGridButtonsPrv = subGridButtons.stream().map(x -> new PrivilegeResourceItem(){{
                        id = x.getRibbonbuttonid();
                        label = x.getLabel();
                        authorizationenabled = x.getAuthorizationenabled();
                        groupid = x.getEntityid();
                    }}).collect(Collectors.toList());
                    PrivilegeResourceItem groupSubGrid = new PrivilegeResourceItem()
                    {{
                        label = "单据体";
                        children = subGridButtonsPrv;
                    }};
                    childrens.add(groupSubGrid);
                }
                group1.children = childrens;
                result.add(group1);
            }
            return JOk(result);
        }
        return JOk();
    }

    @ApiOperation("启用权限")
    @RequestMapping(value = "authorizationenabled", method = RequestMethod.POST)
    public JsonResultObject authorizationEnabled(@RequestBody UpdateAuthorizationStateModel model)
    {
        List<RibbonButton> authorizations = _ribbonButtonFinderService.getAuthorizations();
        if (CollectionUtil.notEmpty(authorizations))
        {
            _ribbonButtonUpdaterService.updateAuthorization(false, authorizations.stream().map(x -> x.getRibbonbuttonid()).collect(Collectors.toList()));
        }
        if (CollectionUtil.notEmpty(model.objectId))
        {
            _ribbonButtonUpdaterService.updateAuthorization(true, model.objectId);
        }
        return JSaveSuccess();
    }
}
